Introduction to Python Day 2

Verjinia Metodieva and Daniel Parthier

2025-02-04

Jupyter Notebook

Recap homework

Let’s take a look at the homework

Functions part 2

Goal of today

# TODO: add useful example to introduce class
# Some function we have to decide
def useful_function(par1, par2, par3):
    # some loop
    for i in par1:
        if par1 == "something":
            # do something
        else:
            pass # or something else
    return None # or an object 

Global vs. Local

Short interlude

  • Whole numbers: Integers int
type(1)
int
  • Real numbers: Floats float
type(1.0)
float
  • Most of the time it might not matter1
1 == 1.0
True
  • Sometimes there is a difference and we will see later why

Conditional statements

The important question of what to do “if” something happens.

  • Programming languages are languages
  • if something is True
    • you should do something
  • else
    • do something else
if statement:
    print("the statement is true")
else:
    print("the statement is false")

Multiple if-statements

value = 3
1if value == 1:
    print("the value is 2")
2elif value == 2:
    print("the value is 2")
3elif value == 3:
4    print("the value is 3")
else:
    print("the value is something else")
1
Check if value is 1
2
Check if value is 2
3
Check if value is 3
4
Execute block
the value is 3

How to check if everything is true?

For loops

Enumerate

Range

List comprehension

Compare different functions

While loops

  • Perform a task while something is True
  • Be careful:
    • Some loops never finish (get stuck)
    • Make sure that condition for ending the loop can be fullfilled
while check_condition:
    perform_task()

Errors and how to read them

Types of errors

Fix errors